package com.fengmi;

import com.fengmi.goods.entity.MallGoods;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FreemarkerDemo {
    public static void main(String[] args) throws Exception {
       //1.构建Configuration对象
        Configuration configuration = new Configuration(Configuration.getVersion());
       //2.设置模板文件目录(Directory)
        configuration.setDirectoryForTemplateLoading(new File("E:\\JAVAEE-2109班\\13.微服务项目实战\\代码\\fenngmi_mall\\fengmi_html\\src\\main\\resources"));
       //3.设置字符集
        configuration.setDefaultEncoding("UTF-8");
       //4.获取模板对象
        Template template = configuration.getTemplate("templates/demo.ftl");
        //5.创建模型数据

        MallGoods mallGoods = new MallGoods();
        mallGoods.setGoodsName("测试1");


        MallGoods mallGoods1 = new MallGoods();
        mallGoods1.setGoodsName("测试2");

        List<MallGoods> goodsList = Arrays.asList(mallGoods, mallGoods1);


        Map data = new HashMap(){{

            put("goodsPrice",1000);
            put("goodsList",goodsList);
            put("salary",10000000);
        }};
       //6.创建输出流(FileWriter)对象
        Writer out = new FileWriter("e:\\demo.html");
       //7.输出
        template.process(data,out);
       //8.关闭
        out.close();

    }
}
